package cn.chinaflame.pagodas.web;

import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.annotation.ApiDoc;
import cn.chinaflame.pagodas.dto.ItemDto;
import cn.chinaflame.pagodas.enums.CodeEnum;
import cn.chinaflame.pagodas.req.ItemAddReq;
import cn.chinaflame.pagodas.service.CommonService;
import cn.chinaflame.pagodas.service.YwItemService;
import cn.chinaflame.pagodas.utils.ParamPreconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.entity.Boy;
import cn.chinaflame.pagodas.repository.BoyRepository;
import cn.chinaflame.pagodas.service.BoyService;
import cn.chinaflame.pagodas.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
/**
 * 项目相关接口
 *
 * @author created by boming
 * @date 2018年1月5日 上午9:45:37
 */
@RestController
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "/openapi/v1/pagodas/item/")
public class OpenItemAPI {

    private static final Logger log = LoggerFactory.getLogger(OpenItemAPI.class);

    @Autowired
    private YwItemService ywItemService;
    @Autowired
    private CommonService commonService;

    /**
     * 查询项目列表
     *
     * @param req
     * @return
     */
    @PostMapping("query/list")
    @ApiDoc(value = ItemAddReq.class, result = ItemDto.class)
    public Object queryList(@RequestBody ItemAddReq req) {
        log.info("查询项目列表req:{}", req);
        if (commonService.paginationSupport(req.getPageIndex(), req.getPageSize())) {
            return ywItemService.queryByPage(req);
        }

        return ywItemService.query(req);
    }

    /**
     * 查询项目
     *
     * @param id
     * @return
     */
    @GetMapping("query/{id}")
    @ApiDoc(ItemDto.class)
    public Object query(@PathVariable String id) {
        log.info("查询单个项目信息id:{}", id);
        ParamPreconditions.checkNotEmpty(id, CodeEnum.FAIL.getCode(), "id不能为空");
        ItemDto dto = ywItemService.querybyId(id);
        ParamPreconditions.checkNotNull(dto, CodeEnum.FAIL.getCode(), "未找到项目信息");
        return Result.create(dto);
    }

    /**
     * 添加单个项目
     *
     * @param req
     * @return
     */
    @PostMapping(value = "add")
    @ApiDoc(value = ItemAddReq.class, result = ItemDto.class)
    public Object add(@RequestBody ItemAddReq req) {
        log.info("添加单个项目req:{}", req);
        req.check();
        ItemDto dto = ywItemService.add(req);
        return Result.create(dto);
    }

    /**
     * 更新单个项目
     *
     * @param data
     * @return
     */
    @PutMapping("update")
    @ApiDoc(value = ItemAddReq.class, result = ItemDto.class)
    public Object update(@RequestBody ItemAddReq req) {
        log.info("更新单个项目req:{}", req);
        req.check();
        ItemDto dto = ywItemService.update(req);
        return Result.create(dto);
    }

    /**
     * 逻辑删除项目信息
     *
     * @param id 项目id
     * @return
     */
    @DeleteMapping("delete/{id}")
    @ApiDoc(Result.class)
    public Object delete(@PathVariable("id") String id) {
        log.info("删除单个项目id:{}", id);
        Boolean b = ywItemService.delete(id);
        return Result.create(b);
    }
}
